package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.Grading;
import com.sl.au.entity.Role;
import com.sl.au.entity.Template;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.TemplateRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.ModuleService;
import com.sl.au.service.TemplateService;

@Service("templateService")
@Transactional
public class TemplateServiceImpl extends BaseServiceImpl implements TemplateService {

	@Autowired
	TemplateRepository templateRepository;
	@Autowired
	UserRepository userRepository;
	
	@Override
	public TemplateRepository getRepository() {
		return this.templateRepository;
	}

	@Override
	public List<Template> getPageByKey(int start, int length, String key, String orderName, String dir) {
		Page<Template> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = templateRepository.getTemplateByKey(key, pageable);
		}else {
			pages = templateRepository.findAll(pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getPageByKeyCount(String key) {
		int count = 0;
		if(StringUtils.hasText(key)) {
			Pageable pageable = new PageRequest( 0, Integer.MAX_VALUE);
			count = templateRepository.getTemplateByKey(key,pageable).getContent().size();
		}else {
			count = templateRepository.findAll().size();
		}
		return count;
	}

	@Override
	public void saveTemplate(Template template) {
		Template old = this.templateRepository.findOne(template.getId());
		if (null != old) {
			BeanUtils.copyProperties(template, old, "createTime");
			this.templateRepository.save(old);
		}else {
			Template newTemplate = new Template();
			newTemplate.setName(template.getName());
			newTemplate.setDescribe(template.getDescribe());
			this.templateRepository.save(newTemplate);
		}
	}

	@Override
	public List<User> getNotSetUserPageByKey(int start, int length, String templateId, String key,
			String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = userRepository.getTemplateNotSetUserPageByKey(templateId, key, pageable);
		}else {
			pages = userRepository.getTemplateNotSetUserPage(templateId, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getNotSetUserPageByKeyCount(String templateId, String key) {
		int count = 0;
		Pageable pageable = new PageRequest( 0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = userRepository.getTemplateNotSetUserPageByKey(templateId, key, pageable).getContent().size();
		}else {
			count = userRepository.getTemplateNotSetUserPage(templateId, pageable).getContent().size();
		}
		return count;
	}

	@Override
	public List<User> getSetUserPageByKey(int start, int length, String templateId, String gradingId, String key,
			String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = userRepository.getTemplateSetUserPageByKey(templateId, gradingId, key, pageable);
		}else {
			pages = userRepository.getTemplateSetUserPage(templateId, gradingId, pageable);
		}
		return pages.getContent();
	}
	
	@Override
	public int getSetUserPageByKeyCount(String templateId, String gradingId, String key) {
		int count = 0;
		Pageable pageable = new PageRequest( 0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = userRepository.getTemplateSetUserPageByKey(templateId, gradingId, key, pageable).getContent().size();
		}else {
			count = userRepository.getTemplateSetUserPage(templateId, gradingId, pageable).getContent().size();
		}
		return count;
	}

}
